#include "TwoDockTutorialLevel.h"

#include "GameplayBackground.h"
#include "il_assert.h"

TwoDockTutorialLevel::~TwoDockTutorialLevel()
{ }

void TwoDockTutorialLevel::Load( Dock* docks, GameplayBackground* background ) 
{
	background->Load( "level1_shore_bg_top", "level1_shore_bg_bottom" );   
	
	// setup the docks
	docks[0].Load( CIwFVec2(229, 39), CIwFVec2(1,0), DockType::RED );
	
	docks[1].Load( CIwFVec2(86, 285), CIwFVec2(-1,0), DockType::YELLOW );
	
	// load the overlays
	m_different_docks_overlay.Load( "different_docks_overlay" );
	
	m_current_state = TwoDockTutorialLevelState::START_ONE;
	m_num_of_active_ships = 0;
}

void TwoDockTutorialLevel::Unload()
{
	m_different_docks_overlay.Unload();
}

// return -1 todo nothing
// return 1 to start a new ship with the provided data
// return 2 to show an overlay message
// return 3 if the level is over
LevelUpdateReturnStatus::Enum TwoDockTutorialLevel::Update( DockType::Enum *type, CIwFVec2* p1, CIwFVec2* p2, float dt )
{
	if( m_current_state == TwoDockTutorialLevelState::START_ONE ) {
		// start the "tutorial boat"
		p1->x = 520;
		p1->y = 200;
		
		p2->x = 480;
		p2->y = 200;
		
		*type = DockType::RED;
		
		m_current_state = TwoDockTutorialLevelState::START_TWO;
		
		return LevelUpdateReturnStatus::START_SHIP;
	}
	
	if( m_current_state == TwoDockTutorialLevelState::START_TWO ) {
		// start the "tutorial boat"
		p1->x = -40;
		p1->y = 200;
		
		p2->x = 00;
		p2->y = 200;
		
		*type = DockType::YELLOW;
		
		m_current_state = TwoDockTutorialLevelState::WAITING_FOR_SHIPS;
		
		return LevelUpdateReturnStatus::START_SHIP;
	}
	
	if( m_current_state == TwoDockTutorialLevelState::WAITING_FOR_SHIPS ) {
		if( m_ship->GetPos().x >= 10 ) {
			m_current_state = TwoDockTutorialLevelState::DOCKS_OVERLAY;
		}
	}
	
	if( m_current_state == TwoDockTutorialLevelState::DOCKS_OVERLAY ) {
		if( m_different_docks_overlay.IsContinuePressed() == true ) {
			m_current_state = TwoDockTutorialLevelState::WAITING_FOR_EXIT;
		}
		
		return LevelUpdateReturnStatus::SHOW_OVERLAY;
	}
	
	if( m_current_state == TwoDockTutorialLevelState::WAITING_FOR_EXIT ) {
		
		if( m_num_of_active_ships == 0 ) {
			return LevelUpdateReturnStatus::LEVEL_COMPLETE;
		}
	}

	return LevelUpdateReturnStatus::DO_NOTHING;
}

void TwoDockTutorialLevel::ShipAdded( Ship *ship ) 
{
	++m_num_of_active_ships; 
	m_ship = ship;
}

void TwoDockTutorialLevel::ShipOutOfScreen( Ship *ship )
{
	--m_num_of_active_ships;
}

void TwoDockTutorialLevel::Draw ()
{
	if( m_current_state == TwoDockTutorialLevelState::DOCKS_OVERLAY ) {
		m_different_docks_overlay.Draw();
	}
}